package com.lysj.website.auth.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.lysj.website.auth.domain.Role;
import com.lysj.website.auth.domain.User;
import com.lysj.website.auth.domain.UserRole;
import com.lysj.website.auth.service.RoleService;
import com.lysj.website.auth.service.UserRoleService;
import com.lysj.website.auth.service.UserService;
import com.lysj.website.common.annotation.SystemLog;
import com.lysj.website.common.exception.BaseException;
import com.lysj.website.common.spring.base.BaseController;
import com.lysj.website.common.spring.pagination.PageVo;
import com.lysj.website.common.web.JwtUtil;
import com.lysj.website.common.web.ParamUtil;
import com.lysj.website.common.web.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author zk
 * @description 用户表控制层
 * @create 2018-07-25 15:02:19
 **/
@Slf4j
@RestController
@RequestMapping("/auth/user")
public class UserController extends BaseController<User> {

    @Resource
    private UserService userService;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RoleService roleService;

    @Value("${secret.header}")
    private String header;


    @Override
    public UserService getService() {
        return userService;
    }


    @Override
    public Resp list(User entity, PageVo pageVo) {
        //获取当前登录用户id
        String header1 = request.getHeader(header);
        String currentUserId = JwtUtil.getPayloadProperty(header1, "sub");
        //判断该用户是否为普通用户,若是普通用户查询自己
        UserRole userRoleQuery = userRoleService.getRepository().findByUserId(currentUserId);
        Role role = roleService.findOne(userRoleQuery.getRoleId());
        Page<User> page = getService().list(entity, pageVo);
        for (User user : page.getContent()) {
            UserRole userRole = userRoleService.getRepository().findByUserId(user.getId());
            user.setLevel(roleService.findOne(userRole.getRoleId()).getName());
        }
        return new Resp().success(page);
    }


    /**
     * @author zk
     * @date 2018-08-13 20:20
     * @Description 添加用户
     */
    @PostMapping("/save_user")
    @SystemLog(description = "添加用户")
    public Resp save(User entity, String roleId) {

        return new Resp().success(userService.saveUser(entity, roleId));
    }

    /**
     * @author zk
     * @date 2018-08-13 21:08
     * @Description 修改用户
     */
    @PostMapping("/update_user")
    @SystemLog(description = "修改用户")
    @Transactional
    public Resp updateUser(User entity) {
        return new Resp().success(userService.updateUser(entity));
    }


    /**
     * @author zk
     * @date 2018-08-13 21:00
     * @Description 禁用或启用
     */
    @PostMapping("/disable_or_enable")
    public Resp disableOrEnable(String id, Integer status) {

        return new Resp().success(userService.disableOrEnable(id, status));
    }




    /*
     * @author drj
     * @date 2019-04-24 11:08
     * @Description ：重置密码
     */
    @PostMapping("/reset_password")
    @SystemLog(description = "重置密码")
    public Resp resetPassword(String id, String password) {
        if (ParamUtil.isBlank(password)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "请输入密码");
        }
        User user = userService.findOne(id);
        if (user == null) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "查无此用户");
        }
        if (!password.matches("^[a-zA-Z0-9_-]{6,20}$")) {
            throw new BaseException("密码应为英文、数字、下划线组成的6-20位字符", Resp.Status.PARAM_ERROR.getCode());
        }
        user.setPassword(BCrypt.hashpw(password));
        userService.save(user);
        return new Resp().success("密码重置成功");
    }


    @Override
    public Resp delete(String[] ids) {
        return null;
    }

    @Override
    public Resp logicalDel(String[] ids) {
        return null;
    }
}
